package com.cloudpark.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudpark.common.util.Constants;
import com.cloudpark.common.util.ObjectUtil;
import com.cloudpark.model.entity.MqttMessage;
import com.cloudpark.model.mapper.MqttMessageMapper;
import com.cloudpark.service.MqttMessageService;

@Service
public class MqttMessageServiceImpl implements MqttMessageService {
	
	@Autowired
	private MqttMessageMapper mqttMessageMapper;

	@Override
	public void save(MqttMessage mqttMessage) {
		mqttMessageMapper.insertSelective(mqttMessage);
	}
	
	@Override
	public MqttMessage save(String uuid, String sendMessage) {
		MqttMessage mqttMessage = new MqttMessage();
		mqttMessage.setUuid(uuid);
		mqttMessage.setSponsor(Constants.MQTT_SPONSOR_CLOUDPARK);
		mqttMessage.setSendMessage(sendMessage);
		mqttMessage.setSendStatus(Constants.MQTT_STATUS_SENDIING);
		mqttMessage.setSendTime(new Date());
		mqttMessage.setCreatedTime(new Date());
		save(mqttMessage);
		return mqttMessage;
	}

	@Override
	public void update(MqttMessage mqttMessage) {
		mqttMessageMapper.updateByPrimaryKey(mqttMessage);
	}

	@Override
	public MqttMessage queryByUuid(String uuid) {
		List<MqttMessage> list = mqttMessageMapper.selectByUuid(uuid);
		if (ObjectUtil.isNotBlank(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public MqttMessage queryById(Long id) {
		return mqttMessageMapper.selectByPrimaryKey(id);
	}
}
